library(data.table)
library(ggplot2)

base <- "/data/workspace/sanction/aamas15-code/output"
dirs <- c("ds1", "ds2", "ds3", "ds4", "ds5", "ds6",
          "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8")
contractF <- "contract.csv"
prosumerF <- "prosumer.csv"
providerF <- "provider.csv"
regulatorF <- "regulator.csv"
replicas <- 5
cycles <- 1000

for(dir in dirs) {
  
  social <- FALSE
  if(substr(dir,1,1) == 's') {
    social <- TRUE
  }
  
  mergeNV <- data.table(cycle=1:cycles-1)
  setkey(mergeNV, cycle)

  mergeNC <- data.table(cycle=1:cycles-1)
  setkey(mergeNC, cycle)

  mergePV <- data.table(cycle=1:cycles-1)
  setkey(mergePV, cycle)

  mergeAPUN <- data.table(cycle=1:cycles-1)
  setkey(mergeAPUN, cycle)

  mergeAINFO <- data.table(cycle=1:cycles-1)
  setkey(mergeAINFO, cycle)

  mergeAPINFO <- data.table(cycle=1:cycles-1)
  setkey(mergeAPINFO, cycle)
  
  mergeRPUN <- data.table(cycle=1:cycles-1)
  setkey(mergeRPUN, cycle)
  
  mergeRINFO <- data.table(cycle=1:cycles-1)
  setkey(mergeRINFO, cycle)
  
  mergeRPINFO <- data.table(cycle=1:cycles-1)
  setkey(mergeRPINFO, cycle)
  
  mergeADEN <- data.table(cycle=1:cycles-1)
  setkey(mergeADEN, cycle)
  
  mergeAREP <- data.table(cycle=1:cycles-1)
  setkey(mergeAREP, cycle)
  
  mergeRREP <- data.table(cycle=1:cycles-1)
  setkey(mergeRREP, cycle)
  
  mergeRSUS <- data.table(cycle=1:cycles-1)
  setkey(mergeRSUS, cycle)

  mergeQS <- data.table(cycle=1:cycles-1)
  setkey(mergeQS, cycle)

  mergeQB <- data.table(cycle=1:cycles-1)
  setkey(mergeQB, cycle)

  for(replica in 0:(replicas-1)) {
    ##
    ## CONTRACT
    ##
    filename <- paste0(base,"/",dir,"/",replica,"/",contractF)
    contractD <- data.table(read.csv(filename, header=TRUE, sep=";",
                                   quote="\""))
    setkey(contractD, cycle)
  
    aux <- contractD[, numV:=nrow(.SD[which(sellerComplied == "false")]), by=cycle]
    aux <- contractD[, numC:=nrow(.SD[which(sellerComplied == "true")]), by=cycle]
    aux <- contractD[, prop:=nrow(.SD[which(sellerComplied == "false")]) / .N, by=cycle]
    
    meanNV <- aux[, mean(numV), by=cycle]
    setnames(meanNV, c("cycle","V1"),c("cycle",paste0("V",replica)))
    setkey(meanNV, cycle)
    mergeNV <- merge(mergeNV, meanNV, all = FALSE)
    
    meanNC <- aux[, mean(numC), by=cycle]
    setnames(meanNC, c("cycle","V1"),c("cycle",paste0("V",replica)))
    setkey(meanNC, cycle)
    mergeNC <- merge(mergeNC, meanNC, all = FALSE)
    
    meanPV <- aux[, mean(prop), by=cycle]
    setnames(meanPV, c("cycle","V1"),c("cycle",paste0("V",replica)))
    setkey(meanPV, cycle)
    mergePV <- merge(mergePV, meanPV, all = FALSE)
    
    ##
    ## PROSUMER
    ##
    filename <- paste0(base,"/",dir,"/",replica,"/",prosumerF)
    prosumerD <- data.table(read.csv(filename, header=TRUE, sep=";",
                                     quote="\""))
    setkey(prosumerD, cycle)
    
    aux <- prosumerD[, apun:=sum(.SD$a_pun_sanction), by=cycle]
    aux <- prosumerD[, apinfo:=sum(.SD$a_pun.info_sanction), by=cycle]
    aux <- prosumerD[, ainfo:=sum(.SD$a_info_sanction), by=cycle]
    aux <- prosumerD[, rpun:=sum(.SD$r_pun_sanction), by=cycle]
    aux <- prosumerD[, rpinfo:=sum(.SD$r_pun.info_sanction), by=cycle]
    aux <- prosumerD[, rinfo:=sum(.SD$r_info_sanction), by=cycle]
        
    meanAPUN <- aux[, mean(apun), by=cycle]
    setnames(meanAPUN, c("cycle","V1"),c("cycle",paste0("V",replica)))
    mergeAPUN <- merge(mergeAPUN, meanAPUN, all = FALSE)
    
    meanAPINFO <- aux[, mean(apinfo), by=cycle]
    setnames(meanAPINFO, c("cycle","V1"),c("cycle",paste0("V",replica)))
    mergeAPINFO <- merge(mergeAPINFO, meanAPINFO, all = FALSE)
  
    meanAINFO <- aux[, mean(ainfo), by=cycle]
    setnames(meanAINFO, c("cycle","V1"),c("cycle",paste0("V",replica)))
    mergeAINFO <- merge(mergeAINFO, meanAINFO, all = FALSE)
    
    meanRPUN <- aux[, mean(rpun), by=cycle]
    setnames(meanRPUN, c("cycle","V1"),c("cycle",paste0("V",replica)))
    mergeRPUN <- merge(mergeRPUN, meanRPUN, all = FALSE)
    
    meanRPINFO <- aux[, mean(rpinfo), by=cycle]
    setnames(meanRPINFO, c("cycle","V1"),c("cycle",paste0("V",replica)))
    mergeRPINFO <- merge(mergeRPINFO, meanRPINFO, all = FALSE)
    
    meanRINFO <- aux[, mean(rinfo), by=cycle]
    setnames(meanRINFO, c("cycle","V1"),c("cycle",paste0("V",replica)))
    mergeRINFO <- merge(mergeRINFO, meanRINFO, all = FALSE)
    
    if(social){
      aux <- prosumerD[, aden:=sum(.SD$a_den_sanction), by=cycle]
      aux <- prosumerD[, arep:=sum(.SD$a_rep_sanction), by=cycle]
      aux <- prosumerD[, rrep:=sum(.SD$r_rep_sanction), by=cycle]
      aux <- prosumerD[, rsus:=sum(.SD$r_sus_sanction), by=cycle]
      
      meanADEN <- aux[, mean(aden), by=cycle]
      setnames(meanADEN, c("cycle","V1"),c("cycle",paste0("V",replica)))
      mergeADEN <- merge(mergeADEN, meanADEN, all = FALSE)
      
      meanAREP <- aux[, mean(arep), by=cycle]
      setnames(meanAREP, c("cycle","V1"),c("cycle",paste0("V",replica)))
      mergeAREP <- merge(mergeAREP, meanAREP, all = FALSE)
      
      meanRREP <- aux[, mean(rrep), by=cycle]
      setnames(meanRREP, c("cycle","V1"),c("cycle",paste0("V",replica)))
      mergeRREP <- merge(mergeRREP, meanRREP, all = FALSE)
      
      meanRSUS <- aux[, mean(rsus), by=cycle]
      setnames(meanRSUS, c("cycle","V1"),c("cycle",paste0("V",replica)))
      mergeRSUS <- merge(mergeRSUS, meanRSUS, all = FALSE)
    }
    
    ##
    ## PROVIDER
    ##
    filename <- paste0(base,"/",dir,"/",replica,"/",providerF)
    providerD <- data.table(read.csv(filename, header=TRUE, sep=";",
                                     quote="\""))
    setkey(providerD, cycle)
  
    meanQS <- providerD[, mean(quantity_sell), by=cycle]
    setnames(meanQS, c("cycle","V1"),c("cycle",paste0("V",replica)))
    setkey(meanQS, cycle)
    mergeQS <- merge(mergeQS, meanQS, all = FALSE)
  
    meanQB <- providerD[, mean(quantity_buy), by=cycle]
    setnames(meanQB, c("cycle","V1"),c("cycle",paste0("V",replica)))
    setkey(meanQB, cycle)
    mergeQB <- merge(mergeQB, meanQB, all = FALSE)
  }
  
  filename <- paste0(base,"/",dir)
  write.table(mergeNV, file = paste0(filename,"/mergeNV.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeNC, file = paste0(filename,"/mergeNC.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergePV, file = paste0(filename,"/mergePV.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeAPUN, file = paste0(filename,"/mergeAPUN.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeAINFO, file = paste0(filename,"/mergeAINFO.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeAPINFO, file = paste0(filename,"/mergeAPINFO.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeRPUN, file = paste0(filename,"/mergeRPUN.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeRINFO, file = paste0(filename,"/mergeRINFO.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  write.table(mergeRPINFO, file = paste0(filename,"/mergeRPINFO.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
  
  if(social) {
    write.table(mergeADEN, file = paste0(filename,"/mergeADEN.csv"), sep = ";",
                row.names = FALSE, col.names = TRUE, append = FALSE)
    write.table(mergeAREP, file = paste0(filename,"/mergeAREP.csv"), sep = ";",
                row.names = FALSE, col.names = TRUE, append = FALSE) 
    write.table(mergeRREP, file = paste0(filename,"/mergeRREP.csv"), sep = ";",
                row.names = FALSE, col.names = TRUE, append = FALSE) 
    write.table(mergeRSUS, file = paste0(filename,"/mergeRSUS.csv"), sep = ";",
                row.names = FALSE, col.names = TRUE, append = FALSE)
  }
  
  write.table(mergeRSUS, file = paste0(filename,"/mergeQS.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE) 
  write.table(mergeRSUS, file = paste0(filename,"/mergeQB.csv"), sep = ";",
              row.names = FALSE, col.names = TRUE, append = FALSE)
}


##
##
##
filename <- paste0(base,"/",dir)

social <- FALSE
if(substr(dir,1,1) == 's') {
  social <- TRUE
}

mergeNV <- data.table(read.csv(paste0(filename,"/mergeNV.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeNC <- data.table(read.csv(paste0(filename,"/mergeNC.csv"), header=TRUE,
                               sep=";", quote="\""))
mergePV <- data.table(read.csv(paste0(filename,"/mergePV.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeAPUN <- data.table(read.csv(paste0(filename,"/mergeAPUN.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeAINFO <- data.table(read.csv(paste0(filename,"/mergeAINFO.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeAPINFO <- data.table(read.csv(paste0(filename,"/mergeAPINFO.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeRPUN <- data.table(read.csv(paste0(filename,"/mergeRPUN.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeRINFO <- data.table(read.csv(paste0(filename,"/mergeRINFO.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeRPINFO <- data.table(read.csv(paste0(filename,"/mergeRPINFO.csv"), header=TRUE,
                               sep=";", quote="\""))

if(social) {
  mergeADEN <- data.table(read.csv(paste0(filename,"/mergeADEN.csv"), header=TRUE,
                                 sep=";", quote="\""))
  mergeAREP <- data.table(read.csv(paste0(filename,"/mergeAREP.csv"), header=TRUE,
                                 sep=";", quote="\""))
  mergeRREP <- data.table(read.csv(paste0(filename,"/mergeRREP.csv"), header=TRUE,
                                 sep=";", quote="\""))
  mergeRSUS <- data.table(read.csv(paste0(filename,"/mergeRSUS.csv"), header=TRUE,
                                 sep=";", quote="\""))
}

mergeQS <- data.table(read.csv(paste0(filename,"/mergeQS.csv"), header=TRUE,
                               sep=";", quote="\""))
mergeQB <- data.table(read.csv(paste0(filename,"/mergeQB.csv"), header=TRUE,
                               sep=";", quote="\""))

if(replicas > 1) {
  ##
  ## VIOLATION
  ##
  mergePV$mean <- apply(mergePV[,colnames(mergePV)[2:replicas+1], with=FALSE], 1, mean)
  mergePV$sd <- apply(mergePV[,colnames(mergePV)[2:replicas+1], with=FALSE], 1, sd)
  
  ggplot(data = mergePV, aes(x=cycle, y=mean)) +
    geom_line(size = 0.5) +
    geom_ribbon(aes(ymax = mean + sd, ymin = mean - sd),
                alpha = 0.2, colour = 0, linetype = 1) + ylim(0, 1)
  
  mergeNV$mean <- apply(mergeNV[,colnames(mergeNV)[2:replicas+1], with=FALSE], 1, mean)
  mergeNV$sd <- apply(mergeNV[,colnames(mergeNV)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeNC$mean <- apply(mergeNC[,colnames(mergeNC)[2:replicas+1], with=FALSE], 1, mean)
  mergeNC$sd <- apply(mergeNC[,colnames(mergeNC)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeAPUN$mean <- apply(mergeAPUN[,colnames(mergeAPUN)[2:replicas+1], with=FALSE], 1, mean)
  mergeAPUN$sd <- apply(mergeAPUN[,colnames(mergeAPUN)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeAPINFO$mean <- apply(mergeAPINFO[,colnames(mergeAPINFO)[2:replicas+1], with=FALSE], 1, mean)
  mergeAPINFO$sd <- apply(mergeAPINFO[,colnames(mergeAPINFO)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeAINFO$mean <- apply(mergeAINFO[,colnames(mergeAINFO)[2:replicas+1], with=FALSE], 1, mean)
  mergeAINFO$sd <- apply(mergeAINFO[,colnames(mergeAINFO)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeRPUN$mean <- apply(mergeRPUN[,colnames(mergeRPUN)[2:replicas+1], with=FALSE], 1, mean)
  mergeRPUN$sd <- apply(mergeRPUN[,colnames(mergeRPUN)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeRPINFO$mean <- apply(mergeRPINFO[,colnames(mergeRPINFO)[2:replicas+1], with=FALSE], 1, mean)
  mergeRPINFO$sd <- apply(mergeRPINFO[,colnames(mergeRPINFO)[2:replicas+1], with=FALSE], 1, sd)
  
  mergeRINFO$mean <- apply(mergeRINFO[,colnames(mergeRINFO)[2:replicas+1], with=FALSE], 1, mean)
  mergeRINFO$sd <- apply(mergeRINFO[,colnames(mergeRINFO)[2:replicas+1], with=FALSE], 1, sd)
  
  if(social){
    mergeADEN$mean <- apply(mergeADEN[,colnames(mergeADEN)[2:replicas+1], with=FALSE], 1, mean)
    mergeADEN$sd <- apply(mergeADEN[,colnames(mergeADEN)[2:replicas+1], with=FALSE], 1, sd)
    
    mergeAREP$mean <- apply(mergeAREP[,colnames(mergeAREP)[2:replicas+1], with=FALSE], 1, mean)
    mergeAREP$sd <- apply(mergeAREP[,colnames(mergeAREP)[2:replicas+1], with=FALSE], 1, sd)
    
    mergeRREP$mean <- apply(mergeRREP[,colnames(mergeRREP)[2:replicas+1], with=FALSE], 1, mean)
    mergeRREP$sd <- apply(mergeRREP[,colnames(mergeRREP)[2:replicas+1], with=FALSE], 1, sd)
    
    mergeRSUS$mean <- apply(mergeRSUS[,colnames(mergeRSUS)[2:replicas+1], with=FALSE], 1, mean)
    mergeRSUS$sd <- apply(mergeRSUS[,colnames(mergeRSUS)[2:replicas+1], with=FALSE], 1, sd)
  }
  
  ##
  ## QUANTITY SELL
  ##
  mergeQS$mean <- apply(mergeQS[,colnames(mergeQS)[2:replicas+1], with=FALSE], 1, mean)
  mergeQS$sd <- apply(mergeQS[,colnames(mergeQS)[2:replicas+1], with=FALSE], 1, sd)
  
  ggplot(data = mergeQS, aes(x=cycle, y=mean)) +
    geom_line(size = 0.5) +
    geom_ribbon(aes(ymax = mean + sd, ymin = mean - sd),
                alpha = 0.2, colour = 0, linetype = 1)
  
  ##
  ## QUANTITY BUY
  ##
  mergeQB$mean <- apply(mergeQB[,colnames(mergeQB)[2:replicas+1], with=FALSE], 1, mean)
  mergeQB$sd <- apply(mergeQB[,colnames(mergeQB)[2:replicas+1], with=FALSE], 1, sd)
  
  ggplot(data = mergeQB, aes(x=cycle, y=mean)) +
    geom_line(size = 0.5) +
    geom_ribbon(aes(ymax = mean + sd, ymin = mean - sd),
                alpha = 0.2, colour = 0, linetype = 2) +
    ylab('Mean kWh Bought') + xlab('Cycle') +
    theme(axis.title.x = element_text(colour = 'black', size = 16, face = 'bold'),
          axis.title.y = element_text(colour = 'black', size = 16, face = 'bold'),
          axis.text.x = element_text(colour = 'black', size = 12, face = 'bold'),
          axis.text.y = element_text(colour = 'black', size = 12, face = 'bold'),
          axis.line = element_line(colour = 'black', size = 0.5, linetype = 'solid'),
          panel.background = element_rect(fill = "transparent",colour = NA),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_blank())
  
} else {
  ggplot(data = mergePV, aes(x=cycle, y=V0)) +
    geom_line() + ylim(0, 1)
  
  ggplot(data = mergeNV, aes(x=cycle, y=V0)) +
    geom_line()
  
  ggplot(data = mergeNC, aes(x=cycle, y=V0)) +
    geom_line()
  
  ggplot(data = mergeQS, aes(x=cycle, y=V0)) +
    geom_line()
  
  ggplot(data = mergeQB, aes(x=cycle, y=V0)) +
    geom_line()
}

limit <- 900
prop <- mean(mergePV[which(cycle >= limit)]$mean)
nv <- sum(mergeNV[which(cycle >= limit)]$mean)
nc <- sum(mergeNC[which(cycle >= limit)]$mean)
nc / (nc + nv)
nc
nv
sum(mergeQS[which(cycle >= limit)]$mean)
sum(mergeQB[which(cycle >= limit)]$mean)

##
## Norm Salience analysis
##

for(i in 1:10){
prosumerId <- sample(0:99, 1)
prosumerId
ggplot(data = prosumerD[which(prosumer == prosumerId)],
       aes(x = cycle, y = normSalience)) +
  geom_line() + ylim(0, 1)
}

wilcox.test(mergePV[800:899]$mean,mergePV[900:999]$mean,
            alternative="two.sided", paired=FALSE)

##
## Evaluating the distributions in the formulas
##
library(rgl)
library(plot3D)

x <- seq(0,1,0.01)
y <- seq(0,1,0.01)
z <- NULL
w <- NULL
for(i in x) {
  for(j in y) {
    z <- rbind(z, c(i, j, (0.5 * i) + (0.5 * (1 - j))))
  }
}

scatter3D(z[,1],z[,2],z[,3])
plot3d(z[,1],z[,2],z[,3])



ggplot() +
  geom_line(data = bp, aes(x=cycle, y=ds5)) +
  geom_line(data = bp, aes(x=cycle, y=s8))

ggplot() +
  geom_boxplot(data = bp, aes(x=cycle, y=ds5)) +
  geom_boxplot(data = bp, aes(x=cycle, y=s8))
